package com.efast.dmom.mes.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WmsAllocationOrder;
import com.efast.dmom.mes.service.WmsAllocationOrderService;

@Controller
@RequestMapping("/wmsAllocationOrder")
public class WmsAllocationOrderController {
	
	@Autowired
	private WmsAllocationOrderService wmsAllocationOrderService;
	
	
	@RequestMapping("/editOrAdd")
	public @ResponseBody ResultBean purchaseOrderSaveOrEdit (@RequestBody List<WmsAllocationOrder> wmsAllocationOrder) {
		if (wmsAllocationOrder.get(0).getFlag().equals("add")) {
			ResultBean save = wmsAllocationOrderService.batchSave(wmsAllocationOrder);
			return save;
		} else if (wmsAllocationOrder.get(0).getFlag().equals("edit")) {
			ResultBean update = wmsAllocationOrderService.update(wmsAllocationOrder.get(0));
			return update;
		}
		return null;
	}
	@RequestMapping("/confirmOrBatchDelete")
	public @ResponseBody ResultBean confirmOrBatchDelete (@RequestBody List<WmsAllocationOrder> wmsAllocationOrder) {
		if (wmsAllocationOrder.get(0).getFlag().equals("batchConfirm")) {
			ResultBean batchConfirm = new ResultBean(true);
			try {
				batchConfirm = wmsAllocationOrderService.batchConfirm(wmsAllocationOrder);
				return batchConfirm;
			} catch (Exception e) {
				System.out.println(e.getMessage());
				batchConfirm.setMessage(e.getMessage());
				batchConfirm.setSuccess(false);
				return batchConfirm;
			}
		} else if (wmsAllocationOrder.get(0).getFlag().equals("batchDelete")) {
			ResultBean batchDelete = wmsAllocationOrderService.batchDelete(wmsAllocationOrder);
			return batchDelete;
		} else if (wmsAllocationOrder.get(0).getFlag().equals("delete")) {
			ResultBean itemDelete = wmsAllocationOrderService.itemDelete(wmsAllocationOrder);
			return itemDelete;
		}
		return null;
	}
	
	@RequestMapping("/itemSaveOrEdit")
	public @ResponseBody ResultBean itemSaveOrEdit(@RequestBody WmsAllocationOrder wmsAllocationOrder) {
		if (wmsAllocationOrder.getFlag().equals("add")) {
			ResultBean save = wmsAllocationOrderService.save(wmsAllocationOrder);
			return save;
		} else if (wmsAllocationOrder.getFlag().equals("edit")) {
			ResultBean update = wmsAllocationOrderService.update(wmsAllocationOrder);
			return update;
		}
		return null;
		
	}


}
